package com.deli.springboot.service;

import com.deli.springboot.entity.BorrowHistory;
import com.deli.springboot.mapper.BorrowHistoryMapper;
import com.deli.springboot.repository.BorrowHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BorrowHistoryServiceImpl implements BorrowHistoryService {

    @Autowired
    private BorrowHistoryMapper borrowHistoryMapper;

    @Override
    public Integer doAdd(BorrowHistory borrowHistory) {
        if (borrowHistory.getUserId() != 0 && borrowHistory.getUserId() != null) {
            borrowHistory.setStatus(1);//状态为未归还
            borrowHistory.setbDate(new Date());//借时间为当前时间
            Integer result = borrowHistoryMapper.addRecord(borrowHistory);
            return result;
        }
        return -1;
    }

    @Override
    public List<BorrowHistory> findByExample(BorrowHistory borrowHistory) {
        return borrowHistoryMapper.findByExample(borrowHistory);
    }


    public Integer update(Long userId) {
        BorrowHistory borrowHistory = new BorrowHistory();
        borrowHistory.setrDate(new Date());//还书时间
        borrowHistory.setStatus(0);
        borrowHistory.setUserId(userId);
        return borrowHistoryMapper.update(borrowHistory);
    }
}
